{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Examples and Exercises from Think Stats, 2nd Edition\n",
    "\n",
    "http://thinkstats2.com\n",
    "\n",
    "Copyright 2016 Allen B. Downey\n",
    "\n",
    "MIT License: https://opensource.org/licenses/MIT\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function, division\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "import brfss\n",
    "\n",
    "import thinkstats2\n",
    "import thinkplot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The estimation game\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Root mean squared error is one of several ways to summarize the average error of an estimation process."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def RMSE(estimates, actual):\n",
    "    \"\"\"Computes the root mean squared error of a sequence of estimates.\n",
    "\n",
    "    estimate: sequence of numbers\n",
    "    actual: actual value\n",
    "\n",
    "    returns: float RMSE\n",
    "    \"\"\"\n",
    "    e2 = [(estimate-actual)**2 for estimate in estimates]\n",
    "    mse = np.mean(e2)\n",
    "    return np.sqrt(mse)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following function simulates experiments where we try to estimate the mean of a population based on a sample with size `n=7`.  We run `iters=1000` experiments and collect the mean and median of each sample."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Experiment 1\n",
      "rmse xbar 0.3862603150323446\n",
      "rmse median 0.48032836544952223\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "def Estimate1(n=7, iters=1000):\n",
    "    \"\"\"Evaluates RMSE of sample mean and median as estimators.\n",
    "\n",
    "    n: sample size\n",
    "    iters: number of iterations\n",
    "    \"\"\"\n",
    "    mu = 0\n",
    "    sigma = 1\n",
    "\n",
    "    means = []\n",
    "    medians = []\n",
    "    for _ in range(iters):\n",
    "        xs = [random.gauss(mu, sigma) for _ in range(n)]\n",
    "        xbar = np.mean(xs)\n",
    "        median = np.median(xs)\n",
    "        means.append(xbar)\n",
    "        medians.append(median)\n",
    "\n",
    "    print('Experiment 1')\n",
    "    print('rmse xbar', RMSE(means, mu))\n",
    "    print('rmse median', RMSE(medians, mu))\n",
    "    \n",
    "Estimate1()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using $\\bar{x}$ to estimate the mean works a little better than using the median; in the long run, it minimizes RMSE.  But using the median is more robust in the presence of outliers or large errors.\n",
    "\n",
    "\n",
    "## Estimating variance\n",
    "\n",
    "The obvious way to estimate the variance of a population is to compute the variance of the sample, $S^2$, but that turns out to be a biased estimator; that is, in the long run, the average error doesn't converge to 0.\n",
    "\n",
    "The following function computes the mean error for a collection of estimates."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def MeanError(estimates, actual):\n",
    "    \"\"\"Computes the mean error of a sequence of estimates.\n",
    "\n",
    "    estimate: sequence of numbers\n",
    "    actual: actual value\n",
    "\n",
    "    returns: float mean error\n",
    "    \"\"\"\n",
    "    errors = [estimate-actual for estimate in estimates]\n",
    "    return np.mean(errors)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following function simulates experiments where we try to estimate the variance of a population based on a sample with size `n=7`.  We run `iters=1000` experiments and two estimates for each sample, $S^2$ and $S_{n-1}^2$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean error biased -0.1374939469040375\n",
      "mean error unbiased 0.006257061945289593\n"
     ]
    }
   ],
   "source": [
    "def Estimate2(n=7, iters=1000):\n",
    "    mu = 0\n",
    "    sigma = 1\n",
    "\n",
    "    estimates1 = []\n",
    "    estimates2 = []\n",
    "    for _ in range(iters):\n",
    "        xs = [random.gauss(mu, sigma) for i in range(n)]\n",
    "        biased = np.var(xs)\n",
    "        unbiased = np.var(xs, ddof=1)\n",
    "        estimates1.append(biased)\n",
    "        estimates2.append(unbiased)\n",
    "\n",
    "    print('mean error biased', MeanError(estimates1, sigma**2))\n",
    "    print('mean error unbiased', MeanError(estimates2, sigma**2))\n",
    "    \n",
    "Estimate2()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The mean error for $S^2$ is non-zero, which suggests that it is biased.  The mean error for $S_{n-1}^2$ is close to zero, and gets even smaller if we increase `iters`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The sampling distribution\n",
    "\n",
    "The following function simulates experiments where we estimate the mean of a population using $\\bar{x}$, and returns a list of estimates, one from each experiment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def SimulateSample(mu=90, sigma=7.5, n=9, iters=1000):\n",
    "    xbars = []\n",
    "    for j in range(iters):\n",
    "        xs = np.random.normal(mu, sigma, n)\n",
    "        xbar = np.mean(xs)\n",
    "        xbars.append(xbar)\n",
    "    return xbars\n",
    "\n",
    "xbars = SimulateSample()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's the \"sampling distribution of the mean\" which shows how much we should expect $\\bar{x}$ to vary from one experiment to the next."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAEKCAYAAAD9xUlFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAIABJREFUe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ZdjfGLzOy64LU/MLNdG/lnKzlGRUGywUvEZs1cYGb3m9lhNbbd6+77u/sexO7CPqHGtvJgrpoHiU03jRwjAAACAElEQVQzcQGxufFHBHenAuwCjHf3vYBviK3NUC3YKxkLHO3u+wIl1D2XVr2vZ2a7AYOAQ9x9b6ASGBp879XB5IN7AYeZWc25plYEr/0AcFnqH5dI3VQUJOO5+7fAfsBIoAx40sxGBJuPsNjUxR8Qm4dm9xrfOi34+gEw190/D/Y2PuW76aOXuPvrweM/Aj+s9fIHAf2B181sNrF5jHrXETXV6x0V/D1mBc91FLBj8D1nmNm7xKbx2D14zc3+Enx9Byiq47VFGkTnFCQruHslMAOYERSA4WY2hdjU6MXuvsTMrgVa1fi2jcHXqhqPN7c3vzdqzwNTu23Ay+4+pAExU72eAY+5+5VxL2DWh9gewP7BnDoT6/h7VKL3tGwj7SlIxjOzXSx+kZ+9iU12uPkX54rgOP/ArXj6XsGJbIAhwH9qbZ8JHGLBGtDBbK07s3X+CQysMePmdmbWm9gKcmuB1WbWDThuK59fJCV9qpBs0A64x2KLy1cAC4GR7r7KzB4mdrhmETBrK557PrG9joeIzZH/QM2N7l4WHKqabGYtg+6xbMVU4u4+z8zGAi+ZWTO+m3Fzppm9B8wldqjp9fqeR2RbaJZUkTqYWRHwbHCSWiQn6PCRiIhU056CiIhU056CiIhUU1EQEZFqKgoiIlJNRUFERKqpKIiISDUVBRERqfb/Zlk9L7gkTxUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cdf = thinkstats2.Cdf(xbars)\n",
    "thinkplot.Cdf(cdf)\n",
    "thinkplot.Config(xlabel='Sample mean',\n",
    "                 ylabel='CDF')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The mean of the sample means is close to the actual value of $\\mu$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "89.94056816952832"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(xbars)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An interval that contains 90% of the values in the sampling disrtribution is called a 90% confidence interval."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(85.87345905535176, 94.11925824713033)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ci = cdf.Percentile(5), cdf.Percentile(95)\n",
    "ci"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the RMSE of the sample means is called the standard error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.487879588208278"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stderr = RMSE(xbars, 90)\n",
    "stderr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Confidence intervals and standard errors quantify the variability in the estimate due to random sampling."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Estimating rates\n",
    "\n",
    "The following function simulates experiments where we try to estimate the mean of an exponential distribution using the mean and median of a sample. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rmse L 1.075066354067901\n",
      "rmse Lm 1.7723826281429338\n",
      "mean error L 0.315202440018787\n",
      "mean error Lm 0.464767815400564\n"
     ]
    }
   ],
   "source": [
    "def Estimate3(n=7, iters=1000):\n",
    "    lam = 2\n",
    "\n",
    "    means = []\n",
    "    medians = []\n",
    "    for _ in range(iters):\n",
    "        xs = np.random.exponential(1.0/lam, n)\n",
    "        L = 1 / np.mean(xs)\n",
    "        Lm = np.log(2) / thinkstats2.Median(xs)\n",
    "        means.append(L)\n",
    "        medians.append(Lm)\n",
    "\n",
    "    print('rmse L', RMSE(means, lam))\n",
    "    print('rmse Lm', RMSE(medians, lam))\n",
    "    print('mean error L', MeanError(means, lam))\n",
    "    print('mean error Lm', MeanError(medians, lam))\n",
    "    \n",
    "Estimate3()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The RMSE is smaller for the sample mean than for the sample median.\n",
    "\n",
    "But neither estimator is unbiased."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercises"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:**  In this chapter we used $\\bar{x}$ and median to estimate µ, and found that $\\bar{x}$ yields lower MSE. Also, we used $S^2$ and $S_{n-1}^2$ to estimate σ, and found that $S^2$ is biased and $S_{n-1}^2$ unbiased.\n",
    "Run similar experiments to see if $\\bar{x}$ and median are biased estimates of µ. Also check whether $S^2$ or $S_{n-1}^2$ yields a lower MSE."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solution goes here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solution goes here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solution goes here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:** Suppose you draw a sample with size n=10 from an exponential distribution with λ=2. Simulate this experiment 1000 times and plot the sampling distribution of the estimate L. Compute the standard error of the estimate and the 90% confidence interval.\n",
    "\n",
    "Repeat the experiment with a few different values of `n` and make a plot of standard error versus `n`.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solution goes here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solution goes here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:** In games like hockey and soccer, the time between goals is roughly exponential. So you could estimate a team’s goal-scoring rate by observing the number of goals they score in a game. This estimation process is a little different from sampling the time between goals, so let’s see how it works.\n",
    "\n",
    "Write a function that takes a goal-scoring rate, `lam`, in goals per game, and simulates a game by generating the time between goals until the total time exceeds 1 game, then returns the number of goals scored.\n",
    "\n",
    "Write another function that simulates many games, stores the estimates of `lam`, then computes their mean error and RMSE.\n",
    "\n",
    "Is this way of making an estimate biased?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def SimulateGame(lam):\n",
    "    \"\"\"Simulates a game and returns the estimated goal-scoring rate.\n",
    "\n",
    "    lam: actual goal scoring rate in goals per game\n",
    "    \"\"\"\n",
    "    goals = 0\n",
    "    t = 0\n",
    "    while True:\n",
    "        time_between_goals = random.expovariate(lam)\n",
    "        t += time_between_goals\n",
    "        if t > 1:\n",
    "            break\n",
    "        goals += 1\n",
    "\n",
    "    # estimated goal-scoring rate is the actual number of goals scored\n",
    "    L = goals\n",
    "    return L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solution goes here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solution goes here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
